package com.qyhl.module_practice.newhome.scan;


import com.google.gson.Gson;
import com.qyhl.module_practice.common.PracticeUrl;
import com.qyhl.module_practice.common.PracticeUtils;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.common.CommonUtils;

public class PracticeScanModel implements PracticeScanContract.PracticeScanModel {
    private PracticeScanPresenter mPresenter;

    PracticeScanModel(PracticeScanPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void doSign(String phone, String qrCode, final String type) {

        EasyHttp.post(PracticeUrl.POST_PRACTICE_SIGN)
                .params("actQrCode", qrCode)
                .params("phone", phone)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("signType", type)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {

                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            if (type.equals("signIn")) {
                                mPresenter.signSuccess("签到成功！");
                            } else {
                                mPresenter.signSuccess("签退成功！");
                            }
                        } else if (apiResult.getCode() == 308 || apiResult.getCode() == 314) {
                            mPresenter.isSignOut();
                        } else {
                            mPresenter.signError(PracticeUtils.getPracticeMessage(apiResult.getCode()));
                        }
                    }
                });
    }
}
